﻿using System;
using System.Diagnostics;
using System.Text;

namespace TYBase
{
    public static class ExceptionExtend
    {
        public static string Info(this Exception ex)
        {
            var st = new StackTrace(ex, true);
            var frames = st.GetFrames();
            var traceString = new StringBuilder();
            if (frames != null)
            {
                foreach (var frame in frames)
                {
                    if (frame.GetFileLineNumber() < 1)
                        continue;

                    traceString.Append(string.Format("->File:{0}\r\nMethod{1}\r\nLineNo{2}\r\n", frame.GetFileName(), frame.GetMethod().Name, frame.GetFileLineNumber()));
                }
            }
            while (ex != null)
            {
                traceString.Append(string.Format("\r\n->ExcetpitonType:{0}\r\nSource{1}\r\nMessage{2}\r\nStackTrace{3}", ex.GetType(), ex.Source, ex.Message, ex.StackTrace));

                ex = ex.InnerException;
            }

            return traceString.ToString();
        }
    }
}